import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/orderDiscount/widget/offers_title.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/provider/base_provider.dart';
import 'package:boss/ui/widget/calendar/calendar.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/form/select_form_field.dart';
import 'package:boss/ui/widget/radioList/radio_list_picker_util.dart';
import 'package:boss/ui/widget/timeRangePicker/time_range_picker.dart';
import 'package:boss/ui/widget/title_row_button.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class OffersFormFieldCommon<T extends BaseProvider> extends StatelessWidget {
  final String title;

  const OffersFormFieldCommon({
    Key key,
    @required this.title,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        OffersTitle(title: title),
        Padding(
          padding: EdgeInsets.symmetric(
            horizontal: 55.w,
          ),
          child: Consumer<T>(
            builder: (context, provider, child) {
              List<Widget> children = [];

              children.add(BaseTextFormField(
                prefixText: '优惠名称',
                hintText: '请输入',
                initialValue: provider.offerName,
                onChanged: (value) {
                  var provider = Provider.of<T>(
                    context,
                    listen: false,
                  );
                  provider.setOfferName(value);
                },
              ));

              children.add(SelectFormField<int>(
                title: '活动时间',
                items: provider.activityTypes,
                value: provider.activityType,
                onChanged: (value) {
                  provider.setActivityType(value);
                },
              ));

              if (provider.activityType == 1) {
                children.addAll([
                  TitleRowButton(
                    title: '日期',
                    hintText: '请选择',
                    value: provider.dateRangeText,
                    onTap: () async {
                      var result = await showCalendar(
                        context,
                        minDateTime: DateTime.now(),
                        startDateTime: provider.dateRange == null
                            ? null
                            : provider.dateRange[0],
                        endDateTime: provider.dateRange == null
                            ? null
                            : provider.dateRange[1],
                      );
                      if (result != null) {
                        provider.setDateRange(result);
                      }
                    },
                  ),
                  TitleRowButton(
                    title: '时间',
                    hintText: '请选择',
                    value: provider.hourRangeText,
                    onTap: () async {
                      var result = await showTimeRangePicker(
                        context,
                        defaultValue: provider.hourRange,
                      );
                      if (result != null) {
                        provider.setHourRange(result);
                      }
                    },
                  ),
                  TitleRowButton(
                    title: '星期',
                    hintText: '请选择',
                    value: provider.weekdayRangeText,
                    onTap: () async {
                      var result = await RadioListPickerUtil.weekday(
                        context,
                        value: provider.weekdayRange,
                      );
                      if (result != null) {
                        provider.setWeekRange(result);
                      }
                    },
                  ),
                ]);
              }

              return Column(
                children: children,
              );
            },
          ),
        ),
      ],
    );
  }
}
